package com.etc.controller;

import javax.servlet.http.HttpSession;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.etc.entity.Users;

@Controller
@SessionAttributes(value = "user") // user对象存放到session范围去
public class SessionAttributeController {

	/**
	 * 这个方法会在请求某个url地址[@RequestMapping]之后会执行
	 * 
	 * @return
	 */
	@ModelAttribute(value = "user")
	public Users getUser() {
		Users u = new Users();
		System.out.println("getUser :" + u);
		return u;
	}
	
	// 模拟登录 传递参数过来就可以
	@RequestMapping("login1")
	public String login(@ModelAttribute(value="user") Users user) {
		// 调用service判断u信息是否正确....
		// 简单模拟就好了
		if (user.getName().equals("admin") && user.getPwd().equals("pwd")) {
			// 不用使用如下 ： 用户登录的信息存储在请求范围内
			// request.getSession().setAttribute("user",user);
			return "main";
		}
		return "login";
	}

	/**
	 * 原生的 servlet添加数据到sessioin的方法
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("request1")
	public String request1(HttpServletRequest request) {
		request.getSession().setAttribute("sessionKey", "sessionValue");
		return "request1";
	}

	@RequestMapping("request2")
	public String request2(HttpSession session) {
		session.setAttribute("sessionKey", "sessionValue");
		return "request2";
	}

	// @SessionAttributes
	// The annotation @SessionAttributes is disallowed for this location
	@RequestMapping("request3")
	public String request3() {
		return "request3";
	}
	




}
